/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.valstorm.model;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author rgrunitzki
 */
@Entity
@Table(name = "users")
@NamedQuery(name = "User.findUserByEmail", query = "select u from User u where u.email = :email")
public class User implements Serializable {

    public static final String FIND_BY_EMAIL = "User.findUserByEmail";

    @Id
    @GeneratedValue
    @Column(name = "id_user", unique = true)
    private long id;

    @Column(name = "ds_name", unique = true, nullable = false, length = 80)
    private String name;

    @Column(name = "ds_email", unique = true, nullable = false, length = 80)
    private String email;
    @Column(name = "ds_password", unique = false, nullable = false, length = 80)
    private String password;

//    @ElementCollection(targetClass = UserType.class)
//    @Enumerated(EnumType.STRING) // Possibly optional (I'm not sure) but defaults to ORDINAL.
//    @CollectionTable(name = "user_categories")
//    @Column(name = "category") // Column name in user_types
//    List<UserType> categories;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "user_projects",
            joinColumns = {
                @JoinColumn(name = "id_user ")},
            inverseJoinColumns = {
                @JoinColumn(name = "id_project")}
    )
    private List<Project> projects;

    @Column(nullable = false)
    private boolean administrator = false;

    @Column(nullable = false)
    private boolean project_controller = false;

    @Column(nullable = false)
    private boolean project_viewer = true;

    public User() {
        this.email = "";
        this.administrator = false;
        this.project_controller = false;
        this.project_viewer = true;
        this.password = "";

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Project> getProjects() {
        return projects;
    }

    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isAdministrator() {
        return administrator;
    }

    public void setAdministrator(boolean administrator) {
        this.administrator = administrator;
    }

    public boolean isProject_controller() {
        return project_controller;
    }

    public void setProject_controller(boolean project_controller) {
        this.project_controller = project_controller;
    }

    public boolean isProject_viewer() {
        return project_viewer;
    }

    public void setProject_viewer(boolean project_viewer) {
        this.project_viewer = project_viewer;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 13 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 13 * hash + Objects.hashCode(this.name);
        hash = 13 * hash + Objects.hashCode(this.email);
        hash = 13 * hash + Objects.hashCode(this.password);
        hash = 13 * hash + Objects.hashCode(this.projects);
        hash = 13 * hash + (this.administrator ? 1 : 0);
        hash = 13 * hash + (this.project_controller ? 1 : 0);
        hash = 13 * hash + (this.project_viewer ? 1 : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.id != other.id) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        if (!Objects.equals(this.email, other.email)) {
            return false;
        }
        if (!Objects.equals(this.password, other.password)) {
            return false;
        }
        if (!Objects.equals(this.projects, other.projects)) {
            return false;
        }
        if (this.administrator != other.administrator) {
            return false;
        }
        if (this.project_controller != other.project_controller) {
            return false;
        }
        if (this.project_viewer != other.project_viewer) {
            return false;
        }
        return true;
    }

}
